package com.jiangyg.devtools;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.TimeUnit;

public class ProgressManagerDemo {

    public void asda(AnActionEvent event) throws InterruptedException {
        ProgressManager.getInstance().run(new Task.Backgroundable(event.getProject(), "Converting bean to JSON...") {
            @Override
            public void run(@NotNull ProgressIndicator indicator) {
                // indicator.setIndeterminate(false);


                try {

                    // 10% done
                    indicator.setFraction(0.1);
                    indicator.setText("90% to finish");

                    System.out.println("1111==============================");

                    TimeUnit.SECONDS.sleep(1);

                    indicator.setFraction(0.50);
                    indicator.setText("50% to finish");
                    System.out.println("2222==============================");

                    TimeUnit.SECONDS.sleep(2);

                    indicator.setFraction(1.0);
                    indicator.setText("finished");
                    System.out.println("3333==============================");

                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }
        });

        TimeUnit.SECONDS.sleep(6);

        System.out.println("==============================");
    }

}
